import { showSecond } from "@/api/index";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imageSrc: "https://tdesign.gtimg.com/mobile/demos/image1.jpeg",
    enable: false,
    currentPage: 1,
    tabList: [
      { id: 0, title: "首页" },
      { id: 1, title: "推荐" },
      { id: 2, title: "热门话题" },
      { id: 3, title: "关注" },
      { id: 4, title: "我的" },
      { id: 5, title: "分类1" },
      { id: 6, title: "分类2" },
      { id: 7, title: "分类3" },
    ],
    currentTab: 0,
    value: "",
    active: 0,
    secondList: [],
    sticky: false,
    showSticky: false,
    categoriesTop: 0,
    categories: [
      "全部",
      "书籍资料",
      "电子数码",
      "洗漱日化",
      "洗漱日化",
      "鞋包服饰",
      "票卡转让",
      "鞋包服饰",
      "票卡转让",
      "其他",
    ],
  },
  priviewImg(e: any) {
    const { index, urls } = e.target.dataset;
    wx.previewImage({
      urls: [urls],
      current: index,
    });
  },
  async onShareAppMessage() {
    return {
      title: "自定义转发标题",
      imageUrl: this.data.tempShareFilePath,
    };
  },
  itemReport(e) {
    const category = `闲置ID：${e.currentTarget.dataset.id.slice(-8)}`;
    wx.navigateTo({
      url: `../me/report/index?category=${encodeURIComponent(category)}`,
    });
  },
  onRefresh() {
    this.setData({ enable: true, currentPage: 1, secondList: [] });
    this.getData();
    this.setData({ enable: false });
  },
  onScroll(e) {
    const { scrollTop } = e.detail;
    this.setData({ scrollTop });
  },
  goPage() {
    wx.navigateTo({
      url: "../second/addItem/index",
    });
  },
  onTabChange(event: WechatMiniprogram.CustomEvent) {
    const { index } = event.detail;
    this.setData({
      secondList: [],
      currentPage: 1,
      currentTab: index,
    });
    this.getData();
	},
	changeSchool(){
		//切换学校
	},
  async reachBottom() {
    this.getData();
	},
	searchFun(){
		this.setData({currentPage: 1, secondList: [] });
    this.getData();
	},
  async getData() {
    const data = await showSecond({
      page: this.data.currentPage,
      search: this.data.value ? this.data.value : "",
      category: this.data.categories[this.data.currentTab],
    });
    console.log(data);
    this.setData({
      currentPage: this.data.currentPage + 1,
      secondList: [...this.data.secondList, ...data],
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getData();
    this.setData({
      currentPage: this.data.currentPage + 1,
    });
  },
  onPageScroll(e) {
    const { categoriesTop, sticky } = this.data;
    if (categoriesTop <= 0) return; // 未初始化
    if (e.scrollTop >= categoriesTop && !sticky) {
      this.setData({ sticky: true, showSticky: true });
    } else if (e.scrollTop < categoriesTop && sticky) {
      this.setData({ sticky: false, showSticky: false });
    }
	},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    wx.createSelectorQuery()
      .select("#categories")
      .boundingClientRect((rect) => {
        if (rect) {
          this.setData({ categoriesTop: rect.top });
        }
      })
      .exec();
  },
});
